package com.smart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.global.GlobalWord;
import com.smart.pojo.ConceptualProduct;
import com.smart.mapper.ConceptualProductMapper;
import com.smart.req.ConceptualReq;
import com.smart.resp.ConceptualResp;
import com.smart.service.IConceptualProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyj
 * @since 2023-05-11
 */
@Service
public class ConceptualProductServiceImpl extends ServiceImpl<ConceptualProductMapper, ConceptualProduct> implements IConceptualProductService {
    @Autowired
    private TaskService taskService;

    @Override
    public void queryByCondition(ConceptualReq conceptualReq, Page<ConceptualResp> page) {
        QueryWrapper<ConceptualResp> wrapper = new QueryWrapper<>();
        wrapper.apply("u.del_flag=0 and cp.del_flag=0")
                .like(!ObjectUtils.isEmpty(conceptualReq.getTitle()), "cp.title", conceptualReq.getTitle())
                .eq(!ObjectUtils.isEmpty(conceptualReq.getUserId()), "cp.user_id", conceptualReq.getUserId());
        baseMapper.queryByCondition(wrapper, page);
        List<ConceptualResp> records = page.getRecords();
        for (ConceptualResp record : records) {
            String businessKey = GlobalWord.DEVELOPED_KEY + "." + record.getId();

            if (record.getStatus() < 4) {
                record.setTaskName(taskService.createTaskQuery().processInstanceBusinessKey(businessKey).singleResult().getName());
            } else {
                record.setTaskName("已成功研发");
            }
        }
    }

    @Override
    public ConceptualResp queryByBillId(long billId) {
        return baseMapper.queryByBillId(billId);
    }
}
